package yartiss.service.model;

import java.util.HashMap;
import java.util.Map;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;

import yartiss.engine.simulation.SimulationStatistics;

public class Metric {
	private final String name;
	private final Map<String, Integer> nbReceivedByPolicy = new HashMap<String, Integer>();
	private final Map<String, Map<String,SimulationStatistics>> policiesStatsPerScenario = new HashMap<String, Map<String, SimulationStatistics>>();
	private final DefaultCategoryDataset  data = new DefaultCategoryDataset (); 
	private final Map<String, XYSeries> curves = new HashMap<String, XYSeries>();
	
	public Metric(String name){
		this.name = name;
	}
	
	
	public void updatePolicyStats(Scenario scenario,String policyName, SimulationStatistics newStats){
		XYSeries series = curves.containsKey(policyName) ? curves.get(policyName) : new  XYSeries(policyName);
		Map<String,SimulationStatistics> scenarioStat = policiesStatsPerScenario.containsKey(scenario.getScenarioName()) ? policiesStatsPerScenario.get(scenario.getScenarioName()) : new HashMap<String,SimulationStatistics>();
		SimulationStatistics stat = scenarioStat.containsKey(policyName) ? scenarioStat.get(policyName) : new SimulationStatistics();
		int nbReceived = nbReceivedByPolicy.containsKey(policyName) ? nbReceivedByPolicy.get(policyName)+1 : 1 ;

		stat.setTimePassedWithEmax(stat.getTimePassedWithEmax()	+ newStats.getTimePassedWithEmax());
		stat.setTimePassedWithEmin(stat.getTimePassedWithEmin()	+ newStats.getTimePassedWithEmin());
		stat.setSimulationDuration(stat.getSimulationDuration()	+ newStats.getSimulationDuration());
		stat.setAverageBusyPeriod(stat.getAverageBusyPeriod()	+ newStats.getAverageBusyPeriod());
		stat.setAverageIdlePeriod(stat.getAverageIdlePeriod()	+ newStats.getAverageIdlePeriod());
		stat.setFailureCount(stat.getFailureCount()		+ newStats.getFailureCount());
		
		if("Failure".equals(name))			data.addValue(stat.getFailureCount(), policyName, scenario.getScenarioName());
		else if("sim duration".equals(name))data.addValue((stat.getSimulationDuration() * 100)	/ (scenario.getSimulationDuration() * nbReceived), policyName, scenario.getScenarioName());
		else if("BP".equals(name))			data.addValue((stat.getAverageBusyPeriod()* 100)    / (scenario.getSimulationDuration() * nbReceived), policyName, scenario.getScenarioName());
		else if("IP".equals(name))			data.addValue((stat.getAverageIdlePeriod()* 100)    / (scenario.getSimulationDuration() * nbReceived), policyName, scenario.getScenarioName());
		else if("Tmax".equals(name))		data.addValue((stat.getTimePassedWithEmax() * 100)	/ (scenario.getSimulationDuration() * nbReceived), policyName, scenario.getScenarioName());	
		else if("Tmin".equals(name))		data.addValue((stat.getTimePassedWithEmin() * 100)	/ (scenario.getSimulationDuration() * nbReceived), policyName, scenario.getScenarioName());
		
		curves.put(policyName, series);
		policiesStatsPerScenario.put(scenario.getScenarioName(),scenarioStat );
		scenarioStat.put(policyName, stat);
		nbReceivedByPolicy.put(policyName, nbReceived);
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the data
	 */
	public DefaultCategoryDataset getData() {
		return data;
	}
	
	
}
